<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Admin\Controller;

use Think\Controller;

class ScoreController extends CommonController {
    /***************商户增删改查*****************/
    function storeList() {
        $User = M('Score_store'); // 实例化User对象
         if (!empty($_REQUEST['keys'])) {
            $map['title'] = array(
                'like',
                '%' . $_REQUEST['keys'] . '%'
            );
        }
        $p = getpage($User, $map, C('List_Len'));
        foreach ($map as $key => $val) {
            $Page->parameter .= "$key=" . urlencode($val) . "&";
        }
        $list = $User->where($map)->order('id asc')->select();
        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $p->show()); // 赋值分页输出
        $this->display('storelist');
    }

    function storeAdd() {
        $id = $_REQUEST['id'];
        $gc = M('Score_store');
        $where['id'] = $id;
        $data = $gc->where($where)->find();
        //echo $gc->getLastSql();exit;
        $this->assign('data', $data);
        $this->display('storeadd');
    }

    function doStore() {
        $gc = M('Score_store');
        if (false === $data = $gc->create()) {
            $this->error($gc->error());
        }
        $id = $_REQUEST['id'];
        if ($id) {
            $data['utime'] = $this->crtTime();
            $state = $gc->where('id=' . $id)->save($data);
        } else {
            $data['ctime'] = $data['utime'] = $this->crtTime();
            $state = $gc->add($data);
        }
        if ($state) {
            $this->success('数据已保存！', "storeList");
        } else {
            $this->error('数据未保存！');
        }
    }
    
    function storeDel(){
        
    }
    
    /***************结束商户增删改查*****************/
    
    
    
    
    
    /***************商户商品增删改查*****************/
    
    function productList() {
        if (!empty($_REQUEST['keys'])) {
            $map['title'] = array(
                'like',
                '%' . $_REQUEST['keys'] . '%'
            );
        }
        $map['sid'] = $_REQUEST['id'];
        $User = M('Score_product'); // 实例化User对象
        $p = getpage($User, $map, C('List_Len'));
        foreach ($map as $key => $val) {
            $Page->parameter .= "$key=" . urlencode($val) . "&";
        }

        $list = $User->where($map)->order('issale desc,id asc')->select();
        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $p->show()); // 赋值分页输出
        $this->getStoreNameById($_REQUEST['id']);//显示商户名
        $this->display('productlist');
    }

    function productAdd() {
        $id = $_REQUEST['id'];
        $gc = M('Score_product');
        $where['id'] = $id;
        $data = $gc->where($where)->find();
        //echo $gc->getLastSql();exit;
        $this->assign('data', $data);
        $this->display('productadd');
    }
    //处理增加修改逻辑
    function doProduct() {
        $gc = M('Score_product');
        if (false === $data = $gc->create()) {
            $this->error($gc->error());
        }
        $data['issale'] = $_POST['issale'] ? $_POST['issale'] : 0;
        $id = $_REQUEST['id'];
        if ($id) {
            $data['utime'] = $this->crtTime();
            $state = $gc->where('id=' . $id)->save($data);
        } else {
            $data['ctime'] = $data['utime'] = $this->crtTime();
            $state = $gc->add($data);
        }
        if ($state) {
            $this->success('数据已保存！', "productList?id=".$data['sid']);
        } else {
            $this->error('数据未保存！');
        }
    }
    //删除商品
    function productDel(){
        $map['id'] = $_REQUEST['id'];
        $gc = M('Score_product');
       // $state = $gc->where($map)->setField('status', 3);
        $state = $gc->where($map)->delete();
        if (!empty($state)) {
            $this->success('数据删除成功！', 'productList?id='.$_REQUEST['sid']);
        } else {
            $this->error('数据删除失败！');
        }
    }

    /***************结束商户商品增删改查*****************/
    
    
    
    
    
    
    
    
    
    /***************商户用户增删改查*****************/
    
    function userList() {
        if (!empty($_REQUEST['keys'])) {
            $map['name'] = array(
                'like',
                '%' . $_REQUEST['keys'] . '%'
            );
        }
        $map['sid'] = $_REQUEST['id'];
        $User = M('Score_storeuser'); // 实例化User对象
        $p = getpage($User, $map, C('List_Len'));
        foreach ($map as $key => $val) {
            $Page->parameter .= "$key=" . urlencode($val) . "&";
        }
        $list = $User->where($map)->order('id asc')->select();
        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $p->show()); // 赋值分页输出
        $this->getStoreNameById($_REQUEST['id']);//显示商户名
        $this->display('userlist');
    }

    function userAdd() {
        $id = $_REQUEST['id'];
        $gc = M('Score_storeuser');
        $where['id'] = $id;
        $data = $gc->where($where)->find();
        //echo $gc->getLastSql();exit;
        $this->assign('data', $data);
        $this->display('useradd');
    }

    function doUser() {
        $gc = M('Score_storeuser');
        if (false === $data = $gc->create()) {
            $this->error($gc->error());
        }
        $id = $_REQUEST['id'];
        if ($id) {
            $data['utime'] = $this->crtTime();
            $state = $gc->where('id=' . $id)->save($data);
        } else {
            $data['ctime'] = $data['utime'] = $this->crtTime();
            $state = $gc->add($data);
        }
        if ($state) {
            $this->success('数据已保存！', "userList?id=".$data['sid']);
        } else {
            $this->error('数据未保存！');
        }
    }
    
    function userDel(){
        $map['id'] = $_REQUEST['id'];
        $gc = M('Score_storeuser');
       // $state = $gc->where($map)->setField('status', 3);
        $state = $gc->where($map)->delete();
        if (!empty($state)) {
            $this->success('数据删除成功！', 'userList?id='.$_REQUEST['sid']);
        } else {
            $this->error('数据删除失败！');
        }
    }
    
    /***************结束商户用户增删改查*****************/

    
    /****************************商户订单***************************/
    
    function orderList(){
       
       // $sid = !empty($_REQUEST['id']) ? $_REQUEST['id'] : str_replace("?sid=", "", base64_decode(I('path.2')));
        $sid  = $_REQUEST['id'];
        $map['sid'] = $sid;
         if (!empty($_REQUEST['keys'])) {
            $map['id'] = array(
                'eq',
                $_REQUEST['keys'] 
            );
        }
        if (!empty($_REQUEST['stime'])) {
             $map['ctime'] =  array('between',array($_REQUEST['stime'],$_REQUEST['etime'].'  23:59:59'));
        }
        $User = M('Score_order'); // 实例化User对象
        $p = getpage($User, $map, C('List_Len'));
        foreach ($map as $key => $val) {
            $Page->parameter .= "$key=" . urlencode($val) . "&";
        }
        $list = $User->where($map)->order('status desc,ctime desc')->select();
        //echo $User->getLastSql();exit;
        if($sid!=$_SESSION['sid'] && $_SESSION['USER']!='admin'){
           $this->error('非法参数');
        }
        // echo $User->getLastSql();exit;
        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $p->show()); // 赋值分页输出
        $this->getStoreNameById($sid);//显示商户名
        $this->display('orderlist');
    }
    
    
       /**
     * 导出数据
     */
    public function exportData() {
       // $arr = $_REQUEST['arr'];
        
        $m = M('Score_order');
        $sid  = $_REQUEST['id'];
        $map['sid'] = $sid;
         if (!empty($_REQUEST['keys'])) {
            $map['id'] = array(
                'eq',
                $_REQUEST['keys'] 
            );
        }
        if (!empty($_REQUEST['stime'])) {
             $map['ctime'] =  array('between',array($_REQUEST['stime'],$_REQUEST['etime'].'  23:59:59'));
        }
        //$where['Admin.status'] = 2;
        $list = $m->where($map)->order('ctime desc')->select();
		//echo count($list);exit;
        foreach ($list as $key => $value) {
            $list[$key]['xuhao'] = $key + 1;
        }
        $title = array('序号','订单号', '购买商品', '用户名', '电话', '支付积分', '支付状态(1.未支付 2.已支付)', '支付时间');
        $field = array('xuhao', 'id', 'title', 'huiyuanname', 'huiyuantel', 'score', 'status', 'ctime');
        R('Excel/exportData', array($list, array($title, $field), '订单表'));
    }
    
    
    
    /*******************二维码生成记录列表*****************/
    
    function qrcodeList(){
        $map['sid'] = $_REQUEST['id'];
        $User = M('Score_qrcode'); // 实例化User对象
        $p = getpage($User, $map, C('List_Len'));
        foreach ($map as $key => $val) {
            $Page->parameter .= "$key=" . urlencode($val) . "&";
        }
        $list = $User->where($map)->order('utime desc')->select();
        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $p->show()); // 赋值分页输出
        $this->getStoreNameById($_REQUEST['id']);//显示商户名
        $this->display('qrcodelist');
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    /*********public method*******************/
    private function getStoreNameById($id){
        $title =  M('Score_store')->where('id='.$id)->getField('title');
        $this->assign('title',$title);
    }
}
